package com.demo.链表;

/**
 * 牛客链表结构
 * @author 86130
 */
public class ListNode {
    public int val;
    public ListNode next;

    public static ListNode getNKLinkedNode(int... vals) {
        ListNode head = null;
        ListNode current = null;
        for(int val : vals) {
            ListNode tmp = new ListNode();
            tmp.val = val;
            if (head == null) {
                head = tmp;
                current = head;
            } else {
                current.next = tmp;
                current = tmp;
            }
        }
        return head;
    }


    public static void print(ListNode head) {
        if (head == null) {
            return;
        }
        ListNode current = head;
        StringBuilder sb = new StringBuilder();
        while (current != null) {
            sb.append(current.val + " -> ");
            current = current.next;
        }
        String str = sb.toString();
        System.out.println(str.substring(0, str.length() - 4));
    }
}
